package ks.packs.anotherDay;

import java.util.ArrayList;

import android.app.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.telephony.TelephonyManager;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import android.widget.ListView;

import com.admob.android.ads.AdView;
import net.daum.mobilead.MobileAdView;

public class anotherDay extends Activity implements OnClickListener {

	private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
	private ListView mList;
	private String queryToSend = null;
	AdView adViewAdMob;
	MobileAdView adViewAdam;
	MultipleAd mAd;
	Button searchBtn;
	EditText editText;

	ImageView btnEngToKorean;
	ImageView btnKorean;
	ImageView btnKoreanToEng;
	ImageView btnEngToEng;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		btnEngToKorean = (ImageView) findViewById(R.id.btnEngToKorean);
		btnEngToKorean.setOnClickListener(this);

		btnKorean = (ImageView) findViewById(R.id.btnKorean);
		btnKorean.setOnClickListener(this);

		btnKoreanToEng = (ImageView) findViewById(R.id.btnKoreanToEng);
		btnKoreanToEng.setOnClickListener(this);

		btnEngToEng = (ImageView) findViewById(R.id.BtnEngToEng);
		btnEngToEng.setOnClickListener(this);

		String countrySO = setCountryCode();

		adViewAdam = (MobileAdView) findViewById(R.id.adviewAdam);
		adViewAdMob = (AdView) findViewById(R.id.adviewAdmob);

		mAd = new MultipleAd(countrySO, adViewAdMob, adViewAdam);

		if (mAd.isKorea()) {

			mAd.initAdam(false, "308Z0CT12dd46458a1");
			mAd.displayAdam();

		} else {

			mAd.initAdMob(false);
			mAd.displayAdmob();

		}

	}

	public void onClick(View v) {

		Intent intentToSearchView = new Intent(getApplicationContext(),
				SearchView.class);

		if (v == btnEngToKorean) {

			intentToSearchView.putExtra(getString(R.string.SORT_OF_DICTIONARY),
					getString(R.string.ENG_TO_KOREAN));

		} else if (v == btnKorean) {

			intentToSearchView.putExtra(getString(R.string.SORT_OF_DICTIONARY),
					getString(R.string.KOREAN_TO_KOREAN));

		} else if (v == btnKoreanToEng) {

			intentToSearchView.putExtra(getString(R.string.SORT_OF_DICTIONARY),
					getString(R.string.KOREAN_TO_ENG));

		} else if (v == btnEngToEng) {

			intentToSearchView.putExtra(getString(R.string.SORT_OF_DICTIONARY),
					getString(R.string.ENG_TO_ENG));

		}

		startActivity(intentToSearchView);

	}

	private String setCountryCode() {
		TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

		return tm.getSimCountryIso();
	}

	/**
	 * Fire an intent to start the speech recognition activity.
	 */
	private void startVoiceRecognitionActivity() {
		Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);

		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en-US");
		intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
				RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
		intent.putExtra(RecognizerIntent.EXTRA_PROMPT,
				"Please speak an English Word.");
		startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
	}

	/**
	 * Handle the results from the recognition activity.
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {

		if (requestCode == VOICE_RECOGNITION_REQUEST_CODE
				&& resultCode == RESULT_OK) {

			// Fill the list view with the strings the recognizer thought it
			// could have heard

			if (mAd.isKorea()) {

				mAd.displayAdam();

			} else {

				mAd.displayAdmob();
			}

			ArrayList<String> matches = data
					.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);

			mList.setAdapter(new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_single_choice, matches));
			mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);

			mList.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					queryToSend = mList.getItemAtPosition(position).toString();
					Intent intent = new Intent(anotherDay.this, tabView.class);
					intent.putExtra("query", queryToSend);
					startActivity(intent);
				}
			});

		}

		super.onActivityResult(requestCode, resultCode, data);
	}

	protected void onResume() {

		super.onResume();

		if (mAd.isKorea()) {

			mAd.displayAdam();

		} else {

			mAd.displayAdmob();
		}

	}

}
